{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e62e8d8f-818e-44a6-9f03-632fc54fb79c",
   "metadata": {},
   "source": [
    "# GraphRAG-图数据库操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4cd2bed-0b81-47fd-93d9-97e954dbcbae",
   "metadata": {},
   "source": [
    "- pip install py2neo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77923101-48fd-42bf-a4cc-1c37fc818439",
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph, Node, Relationship"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8433d9e5-d4bf-4a71-b4f4-84606e26cd35",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from tqdm import tqdm\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc87754d-5f59-49be-82ce-b6187d2b0a0e",
   "metadata": {},
   "source": [
    "## 连接到Neo4j数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a59e72cf-cc76-465d-bbf4-020339c5c376",
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = Graph(\"bolt://180.xx.26.xx:7687\", user='neo4j', password='neo4j@123',name='neo4j')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4b3d62b-9c96-4590-9641-0c1591579eb5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "57e64118-756a-4d08-b7f5-72febf0929d0",
   "metadata": {},
   "source": [
    "## 解析数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ac6a69f-5a68-4efe-9a96-c05d424defcf",
   "metadata": {},
   "source": [
    "- 公司事件提取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "60501b32-8634-4f75-a826-8ca014c8a0f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "event_type_file = './data/iree.json'\n",
    "event_type_data = []\n",
    "with open(event_type_file, 'r', encoding='utf-8') as file:\n",
    "    for line in file.readlines():\n",
    "        event_type_data.append(json.loads(line.strip()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e7b5dcd1-72cd-46c6-ad5f-f65fc2893bb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'title': '雪佛龙计划大规模裁员 约6000个工作岗位面临风险',\n",
      " 'content': '北京时间28日消息，雪佛龙计划今年将全球员工人数削减10%至15%，这是在新冠肺炎之后全球石油巨头中最大规模的裁员。此番裁员相当于该公司45，000名非加油站员工中的约6，000人，并可能是BP和壳牌等大型石油公司裁员的先兆。截至目前，裁员主要集中在油田服务领域和北美的独立生产商之中。雪佛龙“正在简化我们的组织结构，以反映出效率并与预期的活动水平保持一致，”该公司周三在一份公告中说。“这是一个艰难的决定，我们不会轻易做出这样的决定。”',\n",
      " 'label': [{'event_type': '裁员',\n",
      "            'arguments': [{'主体': '雪佛龙'}, {'时间': '北京时间28日'}]}]}\n"
     ]
    }
   ],
   "source": [
    "pprint(event_type_data[20], sort_dicts=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "727b133b-e2bb-4e89-a376-942585504380",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'title': '科伦药业：控股股东解除1100万股质押再质押1300万股',\n",
      " 'content': '5月26日晚间，科伦药业发布公告称，公司董事长、控股股东刘革新于2020年5月22日质押1300万股，占其所持股份比例为3.43%。2020年5月25日，刘革新将原质押给华泰证券（上海）资产管理有限公司的公司股份1100万股解除质押，占其所持股份比例为2.9%。截至本公告日，刘革新累计质押股数约为2.08亿股，占其所持股份比例为54.78%。',\n",
      " 'label': [{'event_type': '股权质押',\n",
      "            'arguments': [{'时间': '2020年5月25日'},\n",
      "                          {'数值': ['1100万股']},\n",
      "                          {'主体': '科伦药业'}]}]}\n"
     ]
    }
   ],
   "source": [
    "pprint(event_type_data[30], sort_dicts=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e0fae7c-13e7-4341-a269-a621f71bbbeb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b46cae6-79d9-494c-9ae5-ac674d0dab95",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "cf23bef5-4baf-4a6a-bba5-092a9d91f004",
   "metadata": {},
   "source": [
    "- 投资数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "23af4353-5d11-4ed8-a2fb-fabc37ebe42a",
   "metadata": {},
   "outputs": [],
   "source": [
    "invest_file = './data/invest-on-invent-KG.json'\n",
    "with open(invest_file, 'r', encoding='utf-8') as file:\n",
    "    invest_data = json.load(file)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0ccd6fdb-6d52-49fe-85bf-bd508c6c4d34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'@id': '0',\n",
       " '@type': 'investor',\n",
       " 'name': '瑞华林投资',\n",
       " 'relationship': {'investCompany': [{'@id': '5617',\n",
       "    '@type': 'company',\n",
       "    'round': '新三板定增',\n",
       "    'date': '2016-03-04'}]}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "invest_data['@graph'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ef2f0b89-a8b0-4ac4-a5a9-f72a70a04332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'@id': '6382',\n",
       " '@type': 'company',\n",
       " 'name': '广州中设机器人智能装备股份有限公司',\n",
       " 'alterNames': ['中设机器人智能装备',\n",
       "  '中设机器人智能装备公司',\n",
       "  '广州中设机器人智能装备',\n",
       "  '广州市中设机器人智能装备',\n",
       "  '广州中设机器人智能装备公司',\n",
       "  '广州市中设机器人智能装备公司'],\n",
       " 'establishDate': '2008-07-23',\n",
       " 'address': '广州市黄埔区云埔工业区方达路6号101房',\n",
       " 'relationship': {'applyPatent': []}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "invest_data['@graph'][6382]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "545e69f5-b0df-4ae3-8931-7ecb4bff5c86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'@id': '141764',\n",
       " '@type': 'patent',\n",
       " 'name': '一种基于DLL的软件架构',\n",
       " 'patentType': '发明公布'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "invest_data['@graph'][141764]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89f76e7d-3263-42ea-b440-a7ce87a5ab44",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56aa9678-f502-4734-baf2-610fe430f104",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "043417d4-dec3-4666-b9ab-70ed1edc58f1",
   "metadata": {},
   "source": [
    "## 数据处理\n",
    "   - 投资数据中：投资者和投资公司 公司和专利 id的对应关系\n",
    "   - 投资数据的公司名称和事件数据名称：名称的对应关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0a2a665b-5bf1-47d0-9bd1-42d914391b4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "obj_by_id = {}\n",
    "\n",
    "for ele in invest_data[\"@graph\"]:\n",
    "    obj_by_id[ele[\"@id\"]] = ele"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f2cdcf75-66dc-4be0-86ba-7003cfee0259",
   "metadata": {},
   "outputs": [],
   "source": [
    "ignore_names = ['电器', '600万股', '公司', '135万元', '2018年', '新产业']\n",
    "even_type_company_names = {}\n",
    "for data in event_type_data:\n",
    "    for label in data['label']:\n",
    "        if 'arguments' in label:\n",
    "            for arg in label['arguments']:\n",
    "                for key, value in arg.items():\n",
    "                    if key == \"主体\":\n",
    "                        if value in ignore_names:\n",
    "                            continue\n",
    "                        even_type_company_names[value] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "720ce2cf-c810-4387-941f-c02bc95e26b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小米集团',\n",
       " '小米',\n",
       " '格力电器',\n",
       " '波音公司',\n",
       " '波音',\n",
       " 'GoPro',\n",
       " '得邦照明股份有限公司',\n",
       " '得邦照明',\n",
       " '横店集团得邦工程塑料有限公司',\n",
       " '汉邦高科']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(even_type_company_names.keys())[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6aaaf268-9549-4411-89c1-939875ecc6e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "invest_company_names = []\n",
    "\n",
    "for ele in invest_data[\"@graph\"]:\n",
    "    if ele[\"@type\"] == \"company\":\n",
    "        invest_company_names.append(ele[\"name\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "99a95a18-8c43-4dd8-8c8a-92e754aa5c8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['广州博鳌纵横网络科技有限公司',\n",
       " '福建博思软件股份有限公司',\n",
       " '深圳优地科技有限公司',\n",
       " '杭州群核信息技术有限公司',\n",
       " '昆山希盟自动化科技有限公司',\n",
       " '北京小爱智能科技有限公司',\n",
       " '奇安信科技集团股份有限公司',\n",
       " '北京大米科技有限公司',\n",
       " '北京诸葛找房信息技术有限公司',\n",
       " '苏州奥易克斯汽车电子有限公司']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "invest_company_names[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cf1e1e70-2c11-4545-8cfe-c7bd41bcba53",
   "metadata": {},
   "outputs": [],
   "source": [
    "even_type_company_dict = {}\n",
    "for company in even_type_company_names.keys():\n",
    "    for iv_cname in invest_company_names:\n",
    "        if company in iv_cname:\n",
    "            even_type_company_dict.setdefault(company, []).append(iv_cname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a575a596-6d3a-47ae-8df1-4dff083c170e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小米科技有限责任公司']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "even_type_company_dict['小米']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9b795ecc-ee68-4807-8ef0-3342718e938c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['珠海格力电器股份有限公司']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "even_type_company_dict['格力电器']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1ae4adf7-782a-407f-a389-d783e70320f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['瑞幸咖啡(北京)有限公司']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "even_type_company_dict['瑞幸咖啡']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58ed1676-860f-4842-bfba-dd8428dd1e8d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "db8eeedd-dea1-45b9-a4be-181da422128f",
   "metadata": {},
   "source": [
    "## 构建节点和边\n",
    "- 公司和事件\n",
    "- 公司和投资者\n",
    "- 公司和专利"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "496a5b11-a45f-4614-9e14-ecfbd5f48fe7",
   "metadata": {},
   "outputs": [],
   "source": [
    "for data in tqdm(event_type_data):\n",
    "    title = data['title']\n",
    "    content = data['content']\n",
    "    \n",
    "    for label in data['label']:\n",
    "        event_type = label['event_type']\n",
    "        event_type_node = Node(\"EventType\", name=event_type)\n",
    "        graph.merge(event_type_node, \"EventType\", \"name\")\n",
    "        \n",
    "        if 'arguments' in label:\n",
    "            \n",
    "            cname = ''\n",
    "            tmp_args = {}\n",
    "            for arg in label['arguments']:\n",
    "                for key, value in arg.items():\n",
    "                    if key == \"主体\":\n",
    "                        cname = value\n",
    "                    else:\n",
    "                        tmp_args[key] = value\n",
    "            if cname in even_type_company_dict:\n",
    "                for company_name in even_type_company_dict[cname]:\n",
    "                    company_node = Node(\"Company\", name=company_name)\n",
    "                    graph.merge(company_node, \"Company\", \"name\")\n",
    "                    \n",
    "                    relationship = Relationship(company_node, \"HAPPEN\", event_type_node)\n",
    "                    for key, value in tmp_args.items():\n",
    "                        relationship[key] = value\n",
    "                    \n",
    "                    relationship['title'] = title\n",
    "                    relationship['content'] = content\n",
    "                    graph.create(relationship)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8d82f13-b874-4878-8298-441eb14a3adc",
   "metadata": {},
   "outputs": [],
   "source": [
    "for ele in tqdm(invest_data[\"@graph\"]):\n",
    "    if ele[\"@type\"] == \"company\":\n",
    "        name = ele[\"name\"]\n",
    "        alterNames = ele[\"alterNames\"]\n",
    "        establishDate = ele[\"establishDate\"]\n",
    "        address = ele[\"address\"]\n",
    "        applyPatent = ele[\"relationship\"][\"applyPatent\"]\n",
    "        \n",
    "        company_node = Node(\"Company\", name=name, establishDate=establishDate, address=address)\n",
    "        graph.merge(company_node, \"Company\", \"name\")\n",
    "        \n",
    "        if len(applyPatent):\n",
    "            for patent in applyPatent:\n",
    "                patent_name = obj_by_id[patent['@id']]['name']\n",
    "                patent_node = Node(\"Patent\", name=patent_name)\n",
    "                graph.merge(patent_node, \"Patent\", \"name\")\n",
    "                \n",
    "                relationship = Relationship(company_node, \"HAVE\", patent_node)\n",
    "                graph.create(relationship)\n",
    "    elif ele[\"@type\"] == \"investor\":\n",
    "        investor_node = Node(\"Investor\", name=ele['name'])\n",
    "        graph.merge(investor_node, \"Investor\", \"name\")\n",
    "        \n",
    "        invest_company = ele[\"relationship\"][\"investCompany\"]\n",
    "        for invest in invest_company:\n",
    "            acompany_ele = obj_by_id[invest['@id']]\n",
    "            name = acompany_ele[\"name\"]\n",
    "            alterNames = acompany_ele[\"alterNames\"]\n",
    "            establishDate = acompany_ele[\"establishDate\"]\n",
    "            address = acompany_ele[\"address\"]\n",
    "            applyPatent = acompany_ele[\"relationship\"][\"applyPatent\"]\n",
    "            \n",
    "            company_node = Node(\"Company\", name=name, establishDate=establishDate, address=address)\n",
    "            graph.merge(company_node, \"Company\", \"name\")\n",
    "            \n",
    "            relationship = Relationship(investor_node, \"INVEST\", company_node)\n",
    "            relationship['date'] = invest['date']\n",
    "            relationship['round'] = invest['round']\n",
    "            graph.create(relationship)\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6509e2fe-952d-4a80-97a5-ccbe9425a160",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93499fec-32da-4315-9136-3cba038b3069",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f9c973c-489e-45a2-bafa-ca3e9b2b56e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7853220-142f-4399-b8ad-a7009418073f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6bffc16a-409a-4cff-a38a-eeb092d3041f",
   "metadata": {},
   "source": [
    "## MATCH查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "90e1e809-d990-433f-856e-30d95694c274",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '北京真视通科技股份有限公司', 'title': ''}\n",
      "{'name': '上海中锂实业有限公司', 'title': ''}\n",
      "{'name': '浙江南都电源动力股份有限公司', 'title': ''}\n",
      "{'name': '上海科匠信息科技有限公司', 'title': ''}\n",
      "{'name': '安徽晶奇网络科技股份有限公司', 'title': ''}\n",
      "{'name': '阳光城集团股份有限公司', 'title': ''}\n",
      "{'name': '浙江聚力文化发展股份有限公司', 'title': ''}\n",
      "{'name': '青岛万达影视投资有限公司', 'title': '商誉暴雷，业绩亏损，万达电影缘何还能逆势扩张'}\n",
      "{'name': '浙江田中精机股份有限公司', 'title': '0.3折“卖子”给大股东 田中精机被疑利益输送'}\n",
      "{'name': '高斯贝尔数码科技股份有限公司', 'title': '高斯贝尔持续经营能力遭深交所质疑 子公司业绩承诺期业绩均未'}\n",
      "{'name': '中文在线数字出版集团股份有限公司', 'title': '市场动态 监管动态 | 收购子公司未达业绩承诺，中文在线收北京证监局警示函'}\n",
      "{'name': '广东东方精工科技股份有限公司', 'title': '卷进东方精工业绩纠纷，宁德时代称对方的披露严重失实'}\n",
      "{'name': '鄂州二医院有限公司', 'title': '上市公司医院投资项目暴雷 医院业绩暴跌'}\n",
      "{'name': '博瑞生物医药(苏州)股份有限公司', 'title': '博瑞生物签订4项对赌协议被追问 \\xa0 业绩承诺连续三年未完成'}\n",
      "{'name': '云南龙发制药股份有限公司', 'title': '一份对赌协议引发的风险！融资7200万元，这家新三板公司从计划IPO到面临“易主”'}\n",
      "{'name': '北京汉邦高科数字技术股份有限公司', 'title': '重组标的业绩承诺未兑现，汉邦高科将回购股份作抵消'}\n"
     ]
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "MATCH (company)-[r:HAPPEN]->(e:EventType {name: \"业绩承诺未达标\"})\n",
    "RETURN company.name as name,r.title as title\n",
    "\"\"\"\n",
    "\n",
    "# 执行查询\n",
    "results = graph.run(query)\n",
    "for record in results:\n",
    "    print(dict(record))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "efd330a5-e4b6-40b4-9f30-a013542083f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '比亚迪股份有限公司', 'title': '比亚迪上半年新增借款138.7亿 连续三年新增借款超上年净资产20％', 'even': '债务增加'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '比亚迪上半年新增借款138.7亿 连续三年新增借款超上年净资产20％', 'even': '债务增加'}\n",
      "{'name': '比亚迪股份有限公司', 'title': '方向错了？动力电池市场将再掀起磷酸铁锂热潮', 'even': '产品创新'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '方向错了？动力电池市场将再掀起磷酸铁锂热潮', 'even': '产品创新'}\n",
      "{'name': '比亚迪股份有限公司', 'title': '车界：新能源车，成也补贴，败也补贴', 'even': '利润下滑'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '车界：新能源车，成也补贴，败也补贴', 'even': '利润下滑'}\n",
      "{'name': '比亚迪股份有限公司', 'title': '比亚迪(01211.HK)与日野汽车合作研发纯电动商用车', 'even': '业务合作'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '比亚迪(01211.HK)与日野汽车合作研发纯电动商用车', 'even': '业务合作'}\n",
      "{'name': '比亚迪股份有限公司', 'title': '好事连连', 'even': '引进战略投资'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '好事连连', 'even': '引进战略投资'}\n",
      "{'name': '比亚迪股份有限公司', 'title': '比亚迪不单单是家车企，这4个副业个个有名，难怪能成领头羊！', 'even': '企业转型'}\n",
      "{'name': '深圳比亚迪电动汽车投资有限公司', 'title': '比亚迪不单单是家车企，这4个副业个个有名，难怪能成领头羊！', 'even': '企业转型'}\n"
     ]
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "MATCH (c:Company)-[r:HAPPEN]->(e)\n",
    "WHERE c.name CONTAINS \"比亚迪\"\n",
    "RETURN c.name as name, r.title as title, e.name as even\n",
    "\"\"\"\n",
    "results = graph.run(query)\n",
    "for record in results:\n",
    "    print(dict(record))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dfbd137e-7417-43c4-aa38-858679bcb5b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'iv.name': '方德信'}\n",
      "{'iv.name': '嘉信元德股权投资基金'}\n",
      "{'iv.name': '东方星空投资'}\n",
      "{'iv.name': '诚禧投资'}\n",
      "{'iv.name': '上海合鲸乐宜投顾'}\n",
      "{'iv.name': '廊坊冀财新毅创业引导股权投资基金(有限合伙)'}\n",
      "{'iv.name': '红十月投资'}\n",
      "{'iv.name': '深圳诚成高科'}\n",
      "{'iv.name': '贵州东方世纪投资管理企业(有限合伙)'}\n",
      "{'iv.name': '聚丰博和'}\n",
      "{'iv.name': '尚珹资本'}\n",
      "{'iv.name': '中瑞深圳'}\n"
     ]
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "MATCH (iv:Investor)-[i:INVEST]->(c:Company {name: \"科大讯飞股份有限公司\"})\n",
    "RETURN iv.name\n",
    "\"\"\"\n",
    "\n",
    "results = graph.run(query)\n",
    "for record in results:\n",
    "    print(dict(record))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fed08629-c556-4650-b704-fe33520cc9bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'iv.name': '中瑞深圳', 'c.name': '科大讯飞股份有限公司', 'e.name': '裁员'}\n",
      "{'iv.name': '中瑞深圳', 'c.name': '科大讯飞股份有限公司', 'e.name': '产品创新'}\n",
      "{'iv.name': '中瑞深圳', 'c.name': '科大讯飞股份有限公司', 'e.name': '股份解禁'}\n"
     ]
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "MATCH (iv:Investor {name: \"中瑞深圳\"})-[i:INVEST]->(c)-[:HAPPEN]->(e)\n",
    "RETURN iv.name,c.name,e.name\n",
    "\"\"\"\n",
    "\n",
    "# 执行查询\n",
    "results = graph.run(query)\n",
    "for record in results:\n",
    "    print(dict(record))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ab235df-82b2-448b-9d39-2252de3d2744",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b480190c-1705-4203-8fea-f0c1425bab33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'iv.name': '羽信资本', 'other.name': '上海妙克信息科技有限公司'}\n",
      "{'iv.name': '羽信资本', 'other.name': '西安瑜乐文化科技股份有限公司'}\n",
      "{'iv.name': '羽信资本', 'other.name': '深圳市茁壮网络股份有限公司'}\n",
      "{'iv.name': '广州基金', 'other.name': '苏州朗动网络科技有限公司'}\n"
     ]
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "MATCH (other)<-[:INVEST]-(iv:Investor)-[:INVEST]->(c:Company {name: \"杭州格像科技有限公司\"})\n",
    "RETURN iv.name, other.name\n",
    "\"\"\"\n",
    "\n",
    "# 执行查询\n",
    "results = graph.run(query)\n",
    "for record in results:\n",
    "    print(dict(record))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a31097ed-ebfc-47ce-9e53-23d20e811ee6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24a271b5-a697-4255-9cc9-63df3c1151f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "llm"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
